//
//  UIScrollView+YFExtension.m
//  project
//
//  Created by yefei on 2019/4/15.
//  Copyright © 2019 健健. All rights reserved.
//

#import "UIScrollView+YFExtension.h"
#import "YFFullScreenHeader.h"

static NSString *emptyViewKey = @"emptyViewKey";
static NSString *emptyViewOffsetKey = @"TableEmptyViewOffsetKey___Yefei";
static NSString *emptyViewIsFirstLoadKey = @"emptyViewIsFirstLoadKey";

@implementation UIScrollView (YFExtension)

#pragma mark - 空态视图
/**
 空态视图 Setter 方法
 */
- (void)setEmptyView:(UIView *)emptyView {
    if (self.emptyDataSetSource == nil) {
        self.emptyDataSetSource = self;
    }
    if (self.emptyDataSetDelegate == nil) {
        self.emptyDataSetDelegate = self;
    }
    objc_setAssociatedObject(self, &emptyViewKey, emptyView, OBJC_ASSOCIATION_RETAIN);
    
    self.emptyViewIsFirstLoad = YES;
}
/**
 空态视图 Getter 方法
 */
- (UIView *)emptyView {
    return objc_getAssociatedObject(self, &emptyViewKey);
}
/**
 空态视图垂直方向的偏移量 Setter 方法
 */
- (void)setEmptyViewOffset:(CGFloat)emptyViewOffset {
    objc_setAssociatedObject(self, &emptyViewOffsetKey, @(emptyViewOffset), OBJC_ASSOCIATION_ASSIGN);
}

/**
 空态视图垂直方向的偏移量 Getter 方法
 */
- (CGFloat)emptyViewOffset {
    NSNumber *numberValue = objc_getAssociatedObject(self, &emptyViewOffsetKey);
    return numberValue.floatValue;
}
/**
 是否第一次加载空视图 Setter 方法
 */
- (void)setEmptyViewIsFirstLoad:(BOOL)emptyViewIsFirstLoad {
    objc_setAssociatedObject(self, &emptyViewIsFirstLoadKey, @(emptyViewIsFirstLoad), OBJC_ASSOCIATION_ASSIGN);
}
/**
 是否第一次加载空视图 Getter 方法
 */
- (BOOL)emptyViewIsFirstLoad {
    return [objc_getAssociatedObject(self, &emptyViewIsFirstLoadKey) boolValue];
}

#pragma mark - DZNEmptyDataSetSource, DZNEmptyDataSetDelegate
- (BOOL)emptyDataSetShouldFadeIn:(UIScrollView *)scrollView
{
    return YES;
}
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
    return self.emptyViewOffset;
}

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView
{
    if (self.emptyView) {
        NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.emptyView
                                                                            attribute:NSLayoutAttributeHeight
                                                                            relatedBy:NSLayoutRelationEqual
                                                                               toItem:nil
                                                                            attribute:NSLayoutAttributeNotAnAttribute
                                                                           multiplier:1
                                                                             constant:[UIScreen mainScreen].bounds.size.height];
        [self.emptyView addConstraint:heightConstraint];
    }
    UIView *emptyView = self.emptyViewIsFirstLoad ? nil : self.emptyView;
    if (self.emptyViewIsFirstLoad) {
        self.emptyViewIsFirstLoad = NO;
    }
    return emptyView;
}


#pragma mark - MJRefresh

/**
 下拉刷新
 @param refreshAction 刷新回调
 */
- (void)refresh:(void(^)(void))refreshAction {
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        if (refreshAction) refreshAction();
    }];
    
    header.stateLabel.font = [UIFont fontWithName:PingFangSC_Medium size:12];
    header.stateLabel.textColor = [UIColor withHex:0X666666];
    header.lastUpdatedTimeLabel.hidden = YES;
    header.arrowView.image = nil;
    self.mj_header = header;
}

/**
 上拉加载
 
 @param loadMoreAction 加载更多
 */
- (void)loadMore:(void(^)(void))loadMoreAction {
    
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (loadMoreAction)  loadMoreAction();
    }];
    footer.stateLabel.font = [UIFont fontWithName:PingFangSC_Medium size:12];
    footer.stateLabel.textColor = [UIColor withHex:0X666666];
    footer.arrowView.image = nil;
    self.mj_footer = footer;
}

/**
 链式调用 下拉刷新
 */
- (YFRefresh)refresh {
    return ^UIScrollView *(YFRefreshAction refreshAction){
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            if (refreshAction) refreshAction();
        }];
        header.stateLabel.font = [UIFont fontWithName:PingFangSC_Medium size:12];
        header.stateLabel.textColor = [UIColor withHex:0X666666];
        header.lastUpdatedTimeLabel.hidden = YES;
        header.arrowView.image = nil;
        self.mj_header = header;
        
        return self;
    };
}

- (YFRefresh)fullScreenRefresh {
    return ^UIScrollView *(YFRefreshAction refreshAction){
        YFFullScreenHeader *header = [YFFullScreenHeader headerWithRefreshingBlock:^{
            if (refreshAction) refreshAction();
        }];
        header.stateLabel.font = [UIFont fontWithName:PingFangSC_Medium size:12];
        header.stateLabel.textColor = [UIColor withHex:0X666666];
        header.lastUpdatedTimeLabel.hidden = YES;
        header.arrowView.image = nil;
        self.mj_header = header;
        
        return self;
    };
}

/**
 链式调用 上拉加载
 */
- (YFRefresh)loadMore {
    return ^UIScrollView *(YFRefreshAction refreshAction){
        MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            if (refreshAction)  refreshAction();
        }];
        footer.stateLabel.font = [UIFont fontWithName:PingFangSC_Medium size:12];
        footer.stateLabel.textColor = [UIColor withHex:0X666666];
        footer.arrowView.image = nil;
        self.mj_footer = footer;
        
        return self;
    };
}

@end
